package com.gaosai.staff.commlistadapter;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.answer_staff.AnswerActivity;
import com.example.answer_staff.R;
import com.gaosai.staff.utils.CommonUtils;
import com.gaosai.staff.view.CustomFlowLayout;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class ShowQuizAdapter extends BaseAdapter {
	//从上一个界面传过来的值是空的，还没有去服务器请求数据。
	private Context context;
	private ArrayList<String> showQuiz_list;
	private LayoutInflater mInflater;
	private String subject_quiz_show;
	private String briefContent_quiz_show;
	private int start_time_quiz_show;
	private int end_time_quiz_show;
	private String venue_quiz_show;
	private JSONArray quizTags;
	private String pic_banner;
	private DisplayImageOptions options;
	public ShowQuizAdapter(Context  context,	ArrayList<String> list) {
		this.context=context;
		this. showQuiz_list=list;
		mInflater = LayoutInflater.from(context) ;
		options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.ic_launcher)
		.showImageForEmptyUri(R.drawable.ic_launcher)
		.showImageOnFail(R.drawable.ic_launcher).cacheInMemory(true)
		.cacheOnDisc(true).considerExifParams(true)
		.bitmapConfig(Bitmap.Config.RGB_565).build();
	
	
	}
	@Override
	public int getCount() {

		return showQuiz_list.size();
	}

	@Override
	public Object getItem(int position) {
		return showQuiz_list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		System.out.println("z的长度是"+showQuiz_list.size());
		String s=showQuiz_list.get(position);
		System.out.println("getViewde s是"+s);
		JsonParse(s);
		ViewHolder  viewholder=null;
		if(convertView==null){
			convertView=LayoutInflater.from(context).inflate(R.layout.activity_item, null);
			viewholder = new ViewHolder();
			viewholder.pic_quiz=(ImageView)convertView.findViewById(R.id.imageView_activity);
			viewholder.subject_quiz = (TextView) convertView.findViewById(R.id.title) ;
			viewholder.briefContent_quiz = (TextView) convertView.findViewById(R.id.content_bf) ;
			viewholder.start_time_quiz = (TextView) convertView.findViewById(R.id.time) ;
			viewholder.end_time_quiz = (TextView) convertView.findViewById(R.id.date) ;
			viewholder.venue_quiz = (TextView) convertView.findViewById(R.id.venue) ;
			viewholder.regist_activity=(RelativeLayout)convertView.findViewById(R.id.regis_bottom_listeners_activity);
			viewholder.QuizFlowLayout = (CustomFlowLayout) convertView.findViewById(R.id.activity_flowlayout) ;

			convertView.setTag(viewholder);
		}else{
			viewholder=(ViewHolder)convertView.getTag();
		}
		ImageLoader.getInstance().displayImage(pic_banner, viewholder.pic_quiz,options) ;
		viewholder.subject_quiz.setText(subject_quiz_show);
		viewholder.briefContent_quiz.setText(briefContent_quiz_show);
		viewholder.start_time_quiz.setText(CommonUtils.getDateToStringt(start_time_quiz_show));
		viewholder.end_time_quiz.setText(CommonUtils.getDateToStringt(end_time_quiz_show));
		viewholder.venue_quiz.setText(venue_quiz_show+"");
		viewholder.regist_activity.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent=new Intent(context,AnswerActivity.class);
				intent.putExtra("application_list", showQuiz_list.get(position));
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(intent);
			}
		});

		viewholder.QuizFlowLayout.removeAllViews() ;//先把布局流中的标签清空，再添加，否则出出现 滚动的时候重复添加
		for (int i = 0; i < quizTags.length(); i++) {
			TextView tv = (TextView) mInflater.inflate(R.layout.tv,viewholder.QuizFlowLayout, false);
			String tagName =quizTags.optJSONObject(i).optString("name") ;
			//取得缩写名字
			String abbreName = CommonUtils.getAbbreviation(tagName) ;
			tv.setText(abbreName) ;
			CommonUtils.setColor(abbreName,tv, context) ;
			viewholder.QuizFlowLayout.addView(tv) ;

		}
		return convertView;
	}

	private void JsonParse(String json) {
		JSONObject jsonObject=null;
		try {
			jsonObject = new JSONObject(json);
			pic_banner=jsonObject.getString("banner");
			subject_quiz_show=jsonObject.getString("subject");
			briefContent_quiz_show=jsonObject.getString("briefContent");
			start_time_quiz_show=jsonObject.getInt("startTime");
			end_time_quiz_show=jsonObject.getInt("endTime");
			venue_quiz_show=jsonObject.getString("venue");
			quizTags = jsonObject.optJSONArray("tags") ;
			
			System.out.println("show解析得知是"+subject_quiz_show+briefContent_quiz_show+end_time_quiz_show);
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	class ViewHolder{
		private CustomFlowLayout QuizFlowLayout ;
		private TextView subject_quiz ;
		private TextView briefContent_quiz ;
		private TextView start_time_quiz ;
		private TextView end_time_quiz ;
		private TextView venue_quiz ;	
		private RelativeLayout regist_activity ;
		private ImageView pic_quiz;
		
	}











}
